package com.exam.hibuy.service.impl;

import com.exam.hibuy.common.StringJsonVo;
import com.exam.hibuy.mapper.UserMapper;
import com.exam.hibuy.model.User;
import com.exam.hibuy.service.LoginService;
import com.exam.hibuy.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by admin on 2017/11/30.
 */
@Service("loginService")
@Transactional(rollbackFor = Exception.class)
public class LoginServiceImpl implements LoginService {

    @Value("${key}")
    private String key;

    @Autowired
    private UserMapper userMapper;


    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public StringJsonVo login(String username, String password)throws Exception {
        StringJsonVo stringJsonVo = new StringJsonVo();

        User user = userMapper.findUserByUserName(username);
        if(user != null){
            String dbPassword = user.getPassword();
            if(dbPassword.equals(Md5Util.md5(password, key))){
                stringJsonVo = new StringJsonVo(1,"登陆成功！");
            }else {
                stringJsonVo = new StringJsonVo(3,"用户名或密码错误");
            }
        }else {
            stringJsonVo = new StringJsonVo(3,"用户不存在！");
        }
        return stringJsonVo;
    }
}
